package com.ctms.system.service.impl;

import com.ctms.common.controller.BaseFrontController;
import com.ctms.common.utils.ExcelFormatUtil;
import com.ctms.system.dao.StudentMapper;
import com.ctms.system.domain.UserDO;
import com.ctms.system.service.ExportService;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    StudentMapper studentMapper;

    Logger logger = LoggerFactory.getLogger(ExportServiceImpl.class);

    @Override
    public ResponseEntity<byte[]> exportExcel(List<UserDO> list) {
        try {
            logger.info(">>>>>>>>>>开始导出excel>>>>>>>>>>");
            BaseFrontController baseFrontController = new BaseFrontController();
            return baseFrontController.buildResponseEntity(export(list), "学生表.xls");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>导出excel 异常，原因为：" + e.getMessage());
        }
        return null;
    }

    @Override
    public ResponseEntity<byte[]> exportExcelByClass(List<UserDO> list) {
        try {
            logger.info(">>>>>>>>>>开始导出excel>>>>>>>>>>");
            BaseFrontController baseFrontController = new BaseFrontController();
            return baseFrontController.buildResponseEntity(exportExcelClass(list), "学生表.xls");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>导出excel 异常，原因为：" + e.getMessage());
        }
        return null;
    }

    @Override
    public ResponseEntity<byte[]> exportMyStu(List<UserDO> userDOS) {
        try {
            logger.info(">>>>>>>>>>开始导出excel>>>>>>>>>>");
            BaseFrontController baseFrontController = new BaseFrontController();
            return baseFrontController.buildResponseEntity(exportStu(userDOS), "学生表.xls");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>导出excel 异常，原因为：" + e.getMessage());
        }
        return null;
    }

    @Override
    public ResponseEntity<byte[]> exportMyStuByClass(List<UserDO> userDOS) {
        try {
            logger.info(">>>>>>>>>>开始导出excel>>>>>>>>>>");
            BaseFrontController baseFrontController = new BaseFrontController();
            return baseFrontController.buildResponseEntity(exportByClass(userDOS), "学生表.xls");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>导出excel 异常，原因为：" + e.getMessage());
        }
        return null;
    }

    @Override
    public ResponseEntity<byte[]> exportMyStuByCourse(List<UserDO> userDOS) {
        try {
            logger.info(">>>>>>>>>>开始导出excel>>>>>>>>>>");
            BaseFrontController baseFrontController = new BaseFrontController();
            return baseFrontController.buildResponseEntity(exportByCourse(userDOS), "学生表.xls");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>导出excel 异常，原因为：" + e.getMessage());
        }
        return null;
    }

    @Override
    public ResponseEntity<byte[]> exportScore(List<UserDO> list) {
        try {
            logger.info(">>>>>>>>>>开始导出excel>>>>>>>>>>");
            BaseFrontController baseFrontController = new BaseFrontController();
            return baseFrontController.buildResponseEntity(exportAllScore(list), "学生成绩表.xls");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>导出excel 异常，原因为：" + e.getMessage());
        }
        return null;
    }

    @Override
    public ResponseEntity<byte[]> exportScoreByClass(List<UserDO> list) {
        try {
            logger.info(">>>>>>>>>>开始导出excel>>>>>>>>>>");
            BaseFrontController baseFrontController = new BaseFrontController();
            return baseFrontController.buildResponseEntity(exportAllScoreByClass(list), "学生成绩表.xls");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>导出excel 异常，原因为：" + e.getMessage());
        }
        return null;
    }

    @Override
    public ResponseEntity<byte[]> exportScoreByCourse(List<UserDO> list) {
        try {
            logger.info(">>>>>>>>>>开始导出excel>>>>>>>>>>");
            BaseFrontController baseFrontController = new BaseFrontController();
            return baseFrontController.buildResponseEntity(exportAllScoreByCourse(list), "学生成绩表.xls");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>导出excel 异常，原因为：" + e.getMessage());
        }
        return null;
    }

    private InputStream export(List<UserDO> list) {
        logger.info(">>>>>>>>>>>>>>>>>>>>开始进入导出方法>>>>>>>>>>");
        ByteArrayOutputStream output = null;
        InputStream inputStream1 = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
        SXSSFSheet sheet = (SXSSFSheet) wb.createSheet();
        //创建样式,居中
        CellStyle style = wb.createCellStyle();
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

        // 设置报表头样式
        CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
        CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

        // 每一列字段名
        String[] strs = new String[] { "学号", "姓名", "性别", "邮箱", "联系电话","所在班级"};

        // 字段名所在表格的宽度
        int[] ints = new int[] { 5000, 5000, 5000, 5000, 5000, 5000 };

        // 设置表头样式
        ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);
        logger.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");

        if (list != null && list.size() > 0) {
            logger.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
            for (int i = 0; i < list.size(); i++) {
                UserDO user = list.get(i);
                SXSSFRow row = (SXSSFRow) sheet.createRow(i + 1);
                int j = 0;

                SXSSFCell cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getUsername()); // 姓名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getName()); // 用户名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getSex()==0){
                    cell.setCellValue("男");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }else if (user.getSex() ==1){
                    cell.setCellValue("女");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getEmail()); // 姓
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getMobile()); // 名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getStudentClass()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);

            }
            logger.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
        }
        try {
            output = new ByteArrayOutputStream();
            wb.write(output);
            inputStream1 = new ByteArrayInputStream(output.toByteArray());
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                    if (inputStream1 != null)
                        inputStream1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream1;
    }

    private InputStream exportExcelClass(List<UserDO> list) {
        logger.info(">>>>>>>>>>>>>>>>>>>>开始进入导出方法>>>>>>>>>>");
        ByteArrayOutputStream output = null;
        InputStream inputStream1 = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
        SXSSFSheet sheet = (SXSSFSheet) wb.createSheet();
        //创建样式,居中
        CellStyle style = wb.createCellStyle();
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

        // 设置报表头样式
        CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
        CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

        // 每一列字段名
        String[] strs = new String[] { "学号", "姓名", "性别", "邮箱", "联系电话","所在班级"};

        // 字段名所在表格的宽度
        int[] ints = new int[] { 5000, 5000, 5000, 5000, 5000, 5000 };

        // 设置表头样式
        ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);
        logger.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");

        if (list != null && list.size() > 0) {
            logger.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
            for (int i = 0; i < list.size(); i++) {
                UserDO user = list.get(i);
                SXSSFRow row = (SXSSFRow) sheet.createRow(i + 1);
                int j = 0;

                SXSSFCell cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getUsername()); // 姓名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getName()); // 用户名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getSex()==0){
                    cell.setCellValue("男");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }else if (user.getSex() ==1){
                    cell.setCellValue("女");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getEmail()); // 姓
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getMobile()); // 名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getStudentClass()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);

            }
            logger.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
        }
        try {
            output = new ByteArrayOutputStream();
            wb.write(output);
            inputStream1 = new ByteArrayInputStream(output.toByteArray());
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                    if (inputStream1 != null)
                        inputStream1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream1;
    }



    //所有的
    private InputStream exportStu(List<UserDO> list) {
        logger.info(">>>>>>>>>>>>>>>>>>>>开始进入导出方法>>>>>>>>>>");
        ByteArrayOutputStream output = null;
        InputStream inputStream1 = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
        SXSSFSheet sheet = (SXSSFSheet) wb.createSheet();
        //创建样式,居中
        CellStyle style = wb.createCellStyle();
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

        // 设置报表头样式
        CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
        CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

        // 每一列字段名
        String[] strs = new String[] { "学号", "学生姓名", "性别", "邮箱", "联系电话","所在班级","所属课程"};

        // 字段名所在表格的宽度
        int[] ints = new int[] { 5000, 5000, 5000, 5000, 5000, 5000, 5000 };

        // 设置表头样式
        ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);
        logger.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");

        if (list != null && list.size() > 0) {
            logger.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
            for (int i = 0; i < list.size(); i++) {
                UserDO user = list.get(i);
                SXSSFRow row = (SXSSFRow) sheet.createRow(i + 1);
                int j = 0;

                SXSSFCell cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getUsername()); // 姓名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getName()); // 用户名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getSex()==0){
                    cell.setCellValue("男");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }else if (user.getSex() ==1){
                    cell.setCellValue("女");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getEmail()); // 姓
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getMobile()); // 名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getStudentClass()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getCourseName()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);
            }
            logger.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
        }
        try {
            output = new ByteArrayOutputStream();
            wb.write(output);
            inputStream1 = new ByteArrayInputStream(output.toByteArray());
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                    if (inputStream1 != null)
                        inputStream1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream1;
    }

    //按班级
    private InputStream exportByClass(List<UserDO> list) {
        logger.info(">>>>>>>>>>>>>>>>>>>>开始进入导出方法>>>>>>>>>>");
        ByteArrayOutputStream output = null;
        InputStream inputStream1 = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
        SXSSFSheet sheet = (SXSSFSheet) wb.createSheet();
        //创建样式,居中
        CellStyle style = wb.createCellStyle();
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

        // 设置报表头样式
        CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
        CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

        // 每一列字段名
        String[] strs = new String[] { "学号", "姓名", "性别", "邮箱", "联系电话","所在班级","所属课程"};

        // 字段名所在表格的宽度
        int[] ints = new int[] { 5000, 5000, 5000, 5000, 5000, 5000, 5000 };

        // 设置表头样式
        ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);
        logger.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");

        if (list != null && list.size() > 0) {
            logger.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
            for (int i = 0; i < list.size(); i++) {
                UserDO user = list.get(i);
                SXSSFRow row = (SXSSFRow) sheet.createRow(i + 1);
                int j = 0;

                SXSSFCell cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getUsername()); // 姓名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getName()); // 用户名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getSex()==0){
                    cell.setCellValue("男");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }else if (user.getSex() ==1){
                    cell.setCellValue("女");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getEmail()); // 姓
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getMobile()); // 名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getStudentClass()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getCourseName()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);
            }
            logger.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
        }
        try {
            output = new ByteArrayOutputStream();
            wb.write(output);
            inputStream1 = new ByteArrayInputStream(output.toByteArray());
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                    if (inputStream1 != null)
                        inputStream1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream1;
    }

    //按课程
    private InputStream exportByCourse(List<UserDO> list) {
        logger.info(">>>>>>>>>>>>>>>>>>>>开始进入导出方法>>>>>>>>>>");
        ByteArrayOutputStream output = null;
        InputStream inputStream1 = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
        SXSSFSheet sheet = (SXSSFSheet) wb.createSheet();
        //创建样式,居中
        CellStyle style = wb.createCellStyle();
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

        // 设置报表头样式
        CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
        CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

        // 每一列字段名
        String[] strs = new String[] { "学号", "姓名", "性别", "邮箱", "联系电话","所在班级","所属课程"};

        // 字段名所在表格的宽度
        int[] ints = new int[] { 5000, 5000, 5000, 5000, 5000, 5000, 5000 };

        // 设置表头样式
        ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);
        logger.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");

        if (list != null && list.size() > 0) {
            logger.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
            for (int i = 0; i < list.size(); i++) {
                UserDO user = list.get(i);
                SXSSFRow row = (SXSSFRow) sheet.createRow(i + 1);
                int j = 0;

                SXSSFCell cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getUsername()); // 姓名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getName()); // 用户名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getSex()==0){
                    cell.setCellValue("男");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }else if (user.getSex() ==1){
                    cell.setCellValue("女");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getEmail()); // 姓
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getMobile()); // 名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getStudentClass()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getCourseName()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);
            }
            logger.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
        }
        try {
            output = new ByteArrayOutputStream();
            wb.write(output);
            inputStream1 = new ByteArrayInputStream(output.toByteArray());
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                    if (inputStream1 != null)
                        inputStream1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream1;
    }

    //管理员导出
    private InputStream exportAllScore(List<UserDO> list) {
        logger.info(">>>>>>>>>>>>>>>>>>>>开始进入导出方法>>>>>>>>>>");
        ByteArrayOutputStream output = null;
        InputStream inputStream1 = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
        SXSSFSheet sheet = (SXSSFSheet) wb.createSheet();
        //创建样式,居中
        CellStyle style = wb.createCellStyle();
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

        // 设置报表头样式
        CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
        CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

        // 每一列字段名
        String[] strs = new String[] { "学号", "姓名", "性别", "邮箱", "联系电话","班级","实训课程","成绩"};

        // 字段名所在表格的宽度
        int[] ints = new int[] { 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000 };

        // 设置表头样式
        ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);
        logger.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");

        if (list != null && list.size() > 0) {
            logger.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
            for (int i = 0; i < list.size(); i++) {
                UserDO user = list.get(i);
                SXSSFRow row = (SXSSFRow) sheet.createRow(i + 1);
                int j = 0;

                SXSSFCell cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getUsername()); // 学号
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getName()); // 姓名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);//性别
                if (user.getSex()==0){
                    cell.setCellValue("男");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }else if (user.getSex() ==1){
                    cell.setCellValue("女");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getEmail()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getMobile()); // 联系电话
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getStudentClass()); // 班级
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getCourseName()==null || user.getCourseName()==""){
                    cell.setCellValue("未选课"); // 实训课程
                }else {
                    cell.setCellValue(user.getCourseName()); // 实训课程
                }
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getScore()==null){
                    cell.setCellValue(0+"分"); // 成绩
                }else {
                    cell.setCellValue(user.getScore()+"分"); // 成绩
                }
                cell.setCellStyle(content);
                cell.setCellStyle(style);
            }
            logger.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
        }
        try {
            output = new ByteArrayOutputStream();
            wb.write(output);
            inputStream1 = new ByteArrayInputStream(output.toByteArray());
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                    if (inputStream1 != null)
                        inputStream1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream1;
    }

    private InputStream exportAllScoreByCourse(List<UserDO> list) {
        logger.info(">>>>>>>>>>>>>>>>>>>>开始进入导出方法>>>>>>>>>>");
        ByteArrayOutputStream output = null;
        InputStream inputStream1 = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
        SXSSFSheet sheet = (SXSSFSheet) wb.createSheet();
        //创建样式,居中
        CellStyle style = wb.createCellStyle();
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

        // 设置报表头样式
        CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
        CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

        // 每一列字段名
        String[] strs = new String[] { "学号", "姓名", "性别", "邮箱", "联系电话","班级","实训课程","成绩"};

        // 字段名所在表格的宽度
        int[] ints = new int[] { 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000 };

        // 设置表头样式
        ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);
        logger.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");

        if (list != null && list.size() > 0) {
            logger.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
            for (int i = 0; i < list.size(); i++) {
                UserDO user = list.get(i);
                SXSSFRow row = (SXSSFRow) sheet.createRow(i + 1);
                int j = 0;

                SXSSFCell cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getUsername()); // 学号
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getName()); // 姓名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);//性别
                if (user.getSex()==0){
                    cell.setCellValue("男");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }else if (user.getSex() ==1){
                    cell.setCellValue("女");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getEmail()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getMobile()); // 联系电话
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getStudentClass()); // 班级
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getCourseName()==null || user.getCourseName()==""){
                    cell.setCellValue("未选课"); // 实训课程
                }else {
                    cell.setCellValue(user.getCourseName()); // 实训课程
                }
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getScore()==null){
                    cell.setCellValue(0+"分"); // 成绩
                }else {
                    cell.setCellValue(user.getScore()+"分"); // 成绩
                }
                cell.setCellStyle(content);
                cell.setCellStyle(style);
            }
            logger.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
        }
        try {
            output = new ByteArrayOutputStream();
            wb.write(output);
            inputStream1 = new ByteArrayInputStream(output.toByteArray());
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                    if (inputStream1 != null)
                        inputStream1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream1;
    }

    private InputStream exportAllScoreByClass(List<UserDO> list) {
        logger.info(">>>>>>>>>>>>>>>>>>>>开始进入导出方法>>>>>>>>>>");
        ByteArrayOutputStream output = null;
        InputStream inputStream1 = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
        SXSSFSheet sheet = (SXSSFSheet) wb.createSheet();
        //创建样式,居中
        CellStyle style = wb.createCellStyle();
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);

        // 设置报表头样式
        CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
        CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

        // 每一列字段名
        String[] strs = new String[] { "学号", "姓名", "性别", "邮箱", "联系电话","班级","实训课程","成绩"};

        // 字段名所在表格的宽度
        int[] ints = new int[] { 5000, 5000, 5000, 5000, 5000, 5000, 5000, 5000 };

        // 设置表头样式
        ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);
        logger.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");

        if (list != null && list.size() > 0) {
            logger.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
            for (int i = 0; i < list.size(); i++) {
                UserDO user = list.get(i);
                SXSSFRow row = (SXSSFRow) sheet.createRow(i + 1);
                int j = 0;

                SXSSFCell cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getUsername()); // 学号
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getName()); // 姓名
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);//性别
                if (user.getSex()==0){
                    cell.setCellValue("男");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }else if (user.getSex() ==1){
                    cell.setCellValue("女");
                    cell.setCellStyle(content);
                    cell.setCellStyle(style);
                }

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getEmail()); // 邮箱
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getMobile()); // 联系电话
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(user.getStudentClass()); // 班级
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getCourseName()==null || user.getCourseName()==""){
                    cell.setCellValue("未选课"); // 实训课程
                }else {
                    cell.setCellValue(user.getCourseName()); // 实训课程
                }
                cell.setCellStyle(content);
                cell.setCellStyle(style);

                cell = (SXSSFCell) row.createCell(j++);
                if (user.getScore()==null){
                    cell.setCellValue(0+"分"); // 成绩
                }else {
                    cell.setCellValue(user.getScore()+"分"); // 成绩
                }
                cell.setCellStyle(content);
                cell.setCellStyle(style);
            }
            logger.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
        }
        try {
            output = new ByteArrayOutputStream();
            wb.write(output);
            inputStream1 = new ByteArrayInputStream(output.toByteArray());
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                    if (inputStream1 != null)
                        inputStream1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream1;
    }

}

